package optiondialoges;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.Serializable;

import javax.swing.JComboBox;

import panel.GlyphPanel;


public class ConstantOptionsDialog extends JDialog implements ActionListener,WindowListener,
		Serializable {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel buttonPanel = null;
	private JPanel optionPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JLabel nameLabel = null;
	private JTextField nameTextField = null;
	private JLabel typeLabel = null;
	private JComboBox typeComboBox = null;
	private JLabel fillLabel = null;
	private JComboBox fillComboBox = null;
	private JLabel widthLabel = null;
	private JTextField widthTextField = null;
	private JLabel heightLabel = null;
	private JTextField heightTextField = null;
	private JLabel slicesLabel = null;
	private JTextField slicesTextField = null;
	private String name;
	private String type;
	private String fill;
	private String width;
	private String height;
	private String slices;
	private GlyphPanel panel;

	/**
	 * @param owner
	 */
	public ConstantOptionsDialog(Frame owner, GlyphPanel panel) {
		super(owner);
		this.panel = panel;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(279, 299);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
			jContentPane.add(getOptionPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes buttonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new Insets(0, 10, 10, 10);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.insets = new Insets(0, 10, 10, 10);
			buttonPanel = new JPanel();
			buttonPanel.setLayout(new GridBagLayout());
			buttonPanel.add(getOkButton(), gridBagConstraints);
			buttonPanel.add(getCancelButton(), gridBagConstraints1);
		}
		return buttonPanel;
	}

	/**
	 * This method initializes optionPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOptionPanel() {
		if (optionPanel == null) {
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints13.gridy = 5;
			gridBagConstraints13.weightx = 1.0;
			gridBagConstraints13.insets = new Insets(10, 0, 0, 0);
			gridBagConstraints13.anchor = GridBagConstraints.WEST;
			gridBagConstraints13.gridx = 1;
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.anchor = GridBagConstraints.EAST;
			gridBagConstraints12.insets = new Insets(10, 30, 0, 10);
			gridBagConstraints12.gridy = 5;
			slicesLabel = new JLabel();
			slicesLabel.setText("Slices");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints11.gridy = 4;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.insets = new Insets(10, 0, 0, 0);
			gridBagConstraints11.anchor = GridBagConstraints.WEST;
			gridBagConstraints11.gridx = 1;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.anchor = GridBagConstraints.EAST;
			gridBagConstraints10.insets = new Insets(10, 30, 0, 10);
			gridBagConstraints10.gridy = 4;
			heightLabel = new JLabel();
			heightLabel.setText("Height");
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints9.gridy = 3;
			gridBagConstraints9.weightx = 1.0;
			gridBagConstraints9.anchor = GridBagConstraints.WEST;
			gridBagConstraints9.insets = new Insets(10, 0, 0, 0);
			gridBagConstraints9.gridx = 1;
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 0;
			gridBagConstraints8.insets = new Insets(10, 30, 0, 10);
			gridBagConstraints8.anchor = GridBagConstraints.EAST;
			gridBagConstraints8.gridy = 3;
			widthLabel = new JLabel();
			widthLabel.setText("Width");
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints7.gridy = 2;
			gridBagConstraints7.weightx = 1.0;
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.insets = new Insets(10, 0, 0, 0);
			gridBagConstraints7.gridx = 1;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.insets = new Insets(10, 30, 0, 10);
			gridBagConstraints6.anchor = GridBagConstraints.EAST;
			gridBagConstraints6.gridy = 2;
			fillLabel = new JLabel();
			fillLabel.setText("Fill With");
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.insets = new Insets(10, 0, 0, 0);
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.insets = new Insets(10, 30, 0, 10);
			gridBagConstraints4.anchor = GridBagConstraints.EAST;
			gridBagConstraints4.gridy = 1;
			typeLabel = new JLabel();
			typeLabel.setText("Type");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.insets = new Insets(0, 30, 0, 10);
			gridBagConstraints2.anchor = GridBagConstraints.EAST;
			gridBagConstraints2.gridy = 0;
			nameLabel = new JLabel();
			nameLabel.setText("Name");
			optionPanel = new JPanel();
			optionPanel.setLayout(new GridBagLayout());
			optionPanel.add(nameLabel, gridBagConstraints2);
			optionPanel.add(getNameTextField(), gridBagConstraints3);
			optionPanel.add(typeLabel, gridBagConstraints4);
			optionPanel.add(getTypeComboBox(), gridBagConstraints5);
			optionPanel.add(fillLabel, gridBagConstraints6);
			optionPanel.add(getFillComboBox(), gridBagConstraints7);
			optionPanel.add(widthLabel, gridBagConstraints8);
			optionPanel.add(getWidthTextField(), gridBagConstraints9);
			optionPanel.add(heightLabel, gridBagConstraints10);
			optionPanel.add(getHeightTextField(), gridBagConstraints11);
			optionPanel.add(slicesLabel, gridBagConstraints12);
			optionPanel.add(getSlicesTextField(), gridBagConstraints13);
		}
		return optionPanel;
	}

	/**
	 * This method initializes okButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("Ok");
			okButton.addActionListener(this);
		}
		return okButton;
	}

	/**
	 * This method initializes cancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(this);
		}
		return cancelButton;
	}

	/**
	 * This method initializes nameTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getNameTextField() {
		if (nameTextField == null) {
			nameTextField = new JTextField();
			nameTextField.setPreferredSize(new Dimension(120, 21));
			nameTextField.setText("untitled");
		}
		return nameTextField;
	}

	/**
	 * This method initializes typeComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getTypeComboBox() {
		if (typeComboBox == null) {
			String[] labels = { "8-bit", "16-bit", "32-bit", "RGB" };
			typeComboBox = new JComboBox(labels);
			typeComboBox.setSelectedIndex(0);
			typeComboBox.setPreferredSize(new Dimension(100, 26));
		}
		return typeComboBox;
	}

	/**
	 * This method initializes fillComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getFillComboBox() {
		if (fillComboBox == null) {
			String[] labels = { "White", "Black", "Ramp" };
			fillComboBox = new JComboBox(labels);
			fillComboBox.setPreferredSize(new Dimension(100, 26));
		}
		return fillComboBox;
	}

	/**
	 * This method initializes widthTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getWidthTextField() {
		if (widthTextField == null) {
			widthTextField = new JTextField();
			widthTextField.setPreferredSize(new Dimension(120, 21));
			widthTextField.setText("400");
		}
		return widthTextField;
	}

	/**
	 * This method initializes heightTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getHeightTextField() {
		if (heightTextField == null) {
			heightTextField = new JTextField();
			heightTextField.setPreferredSize(new Dimension(120, 21));
			heightTextField.setText("400");
		}
		return heightTextField;
	}

	/**
	 * This method initializes slicesTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getSlicesTextField() {
		if (slicesTextField == null) {
			slicesTextField = new JTextField();
			slicesTextField.setPreferredSize(new Dimension(120, 21));
			slicesTextField.setText("1");
		}
		return slicesTextField;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if (button.getText().contentEquals("Ok")) {
			name = getNameTextField().getText();
			type = (String) getTypeComboBox().getSelectedItem();
			fill = (String) getFillComboBox().getSelectedItem();
			width = (String) getWidthTextField().getText();
			height = (String) getHeightTextField().getText();
			slices = (String) getSlicesTextField().getText();
			panel.setDialog(this);
			panel.setOptionsSet(true);
			String command;
			command = String
					.format(
							"newImage(\"%s\" , \"%s %s\", %s, %s, %s); ",
							name, type, fill, width, height, slices);
			panel.setCommand(command);
		} else if (button.getText().contentEquals("Cancel")) {
			nameTextField.setText(name);
			typeComboBox.setSelectedItem(type);
			fillComboBox.setSelectedItem(fill);
			widthTextField.setText(width);
			heightTextField.setText(height);
			slicesTextField.setText(slices);
		}
		this.setVisible(false);

	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		nameTextField.setText(name);
		typeComboBox.setSelectedItem(type);
		fillComboBox.setSelectedItem(fill);
		widthTextField.setText(width);
		heightTextField.setText(height);
		slicesTextField.setText(slices);
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		name = getNameTextField().getText();
		type = (String) getTypeComboBox().getSelectedItem();
		fill = (String) getFillComboBox().getSelectedItem();
		width = (String) getWidthTextField().getText();
		height = (String) getHeightTextField().getText();
		slices = (String) getSlicesTextField().getText();
		
	}

} // @jve:decl-index=0:visual-constraint="10,10"
